package Hanoi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class State {
	private int t1[];
	private int t2[];
	private int t3[];
	private double reward = -0.1;
	private double value = 0.0;
	private Map <int[],Double> Q;
	private double q_max_init = 0.0;
	
	public double getQ(int[] a) {
		boolean found=false;
		
		//if (!Q.containsKey(a)) {
		for(int[] b : Q.keySet()) {
			if (Arrays.equals(a, b)) {
				found=true;
				a = b;
			}
		}
			
		if (!found) {
			Q.put(a, 0.0);
		}
		
		return Q.get(a);
	}
	
	public void setQ(int[] a, double v) {
		Q.put(a, v);
	}
	
	public void setQmax(double d) {
		this.q_max_init = d;
	}
	
	public double getQmax() {
		double max=q_max_init;
		
		for (int[] a : Q.keySet()) {
			if (Q.get(a) > max) {
				max = Q.get(a);
			}
		}
		return max;
	}
	
	public int[] getT1() {
		return t1;
	}

	public void setT1(int[] t1) {
		this.t1 = t1;
	}

	public int[] getT2() {
		return t2;
	}

	public void setT2(int[] t2) {
		this.t2 = t2;
	}

	public int[] getT3() {
		return t3;
	}

	public void setT3(int[] t3) {
		this.t3 = t3;
	}

	public double getReward() {
		return reward;
	}
	
	private List<Integer> array_intToInteger(int[] in) {
		List<Integer> ret = new ArrayList<Integer>();
		
		for(int i=0; i<in.length; i++) {
			ret.add(new Integer(in[i]));
		}
		
		return ret;
	}
	
	public List<Integer>[] asList() {
		List<Integer>l1 = array_intToInteger(t1);
		List<Integer>l2 = array_intToInteger(t2);
		List<Integer>l3 = array_intToInteger(t3);
		
		List<Integer>[] ret = new List[]{l1, l2, l3};
		return ret;
	}
	
	public State(int t1[], int t2[], int t3[], double reward) {
		this.t1 = t1;
		this.t2 = t2;
		this.t3 = t3;
		this.reward = reward;
		this.Q = new HashMap<int[], Double>();
	}
	
	public State(int t1[], int t2[], int t3[]) {
		this.t1 = t1;
		this.t2 = t2;
		this.t3 = t3;
		this.Q = new HashMap<int[], Double>();
	}
	
	public boolean equals(Object b) {
		State s2 = (State)b;

		if (Arrays.equals(s2.t1, t1) && Arrays.equals(s2.t2, t2) && Arrays.equals(s2.t3, t3)) return true;
		
		return false;
	}

	public void setValue(double value) {
		this.value = value;
	}

	public double getValue() {
		return value;
	}
	
	public String toString() {
		return Arrays.toString(t1) + Arrays.toString(t2) + Arrays.toString(t3);
	}
}
